package org.fao.pims.web.pollinteractions;

import javax.servlet.http.*;
import org.apache.commons.logging.*;
import org.apache.struts.action.*;
import org.fao.pims.model.*;
import org.fao.pims.service.pollinteractions.*;



public class SpeciesAction extends Action {
	
	private static Log log = LogFactory.getLog(SpeciesAction.class);
	
	public ActionForward execute(
			ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,
			HttpServletResponse response) throws Exception
	{
		
		SpeciesForm speciesForm = (SpeciesForm) form;
		
		SPModel model = null;
		
		InfoItem item = null;
		
		SpeciesInteractionsManager manager = null;
		
		try {
			
			manager = new SpeciesInteractionsManagerImpl();
		
			model = manager.getSpeciesModel(speciesForm.getScientificName());
			
			item = model.getInfoItem();
			
			if(item != null) {
				
				request.setAttribute("species", item);
				
				return mapping.findForward("success");
				
			}
			else {
				
				
				ActionMessages errors = new ActionMessages();
				
				errors.add(
	                    ActionMessages.GLOBAL_MESSAGE,
	                    new ActionMessage("speciesInteractions.nodata.exception"));
				
				this.saveErrors(request, errors);
				
				return mapping.findForward("failure");
				
			}
			
			
		
		}
		catch(SpeciesInteractionsException sie) {
			
			log.error(sie.getMessage());			
			
			ActionMessages errors = new ActionMessages();
			
			errors.add(
                    ActionMessages.GLOBAL_MESSAGE,
                    new ActionMessage("speciesInteractions.exception"));
			
			this.saveErrors(request, errors);
			
			return mapping.findForward("failure");
			
		}
		
		
	}

}
